package proxy.wrappers;

import android.content.ClipData;
import android.content.IOnPrimaryClipChangedListener;
import android.os.Build;
import android.os.IInterface;

import com.wetest.uia2.Ln;

import java.lang.reflect.Method;

/* loaded from: classes2.dex */
public class ClipboardManager {
    private int addListenerMethodVersion;
    private Method addPrimaryClipChangedListener;
    private int getMethodVersion;
    private Method getPrimaryClipMethod;
    private final IInterface manager;
    private int setMethodVersion;
    private Method setPrimaryClipMethod;

    public ClipboardManager(IInterface manager) {
        this.manager = manager;
    }

    private Method getGetPrimaryClipMethod() throws NoSuchMethodException {
        if (this.getPrimaryClipMethod == null) {
            if (Build.VERSION.SDK_INT < 29) {
                this.getPrimaryClipMethod = this.manager.getClass().getMethod("getPrimaryClip", String.class);
            } else {
                try {
                    this.getPrimaryClipMethod = this.manager.getClass().getMethod("getPrimaryClip", String.class, Integer.TYPE);
                    this.getMethodVersion = 0;
                } catch (NoSuchMethodException e) {
                    try {
                        this.getPrimaryClipMethod = this.manager.getClass().getMethod("getPrimaryClip", String.class, String.class, Integer.TYPE);
                        this.getMethodVersion = 1;
                    } catch (NoSuchMethodException e2) {
                        try {
                            this.getPrimaryClipMethod = this.manager.getClass().getMethod("getPrimaryClip", String.class, String.class, Integer.TYPE, Integer.TYPE);
                            this.getMethodVersion = 2;
                        } catch (NoSuchMethodException e3) {
                            try {
                                this.getPrimaryClipMethod = this.manager.getClass().getMethod("getPrimaryClip", String.class, Integer.TYPE, String.class);
                                this.getMethodVersion = 3;
                            } catch (NoSuchMethodException e4) {
                                try {
                                    this.getPrimaryClipMethod = this.manager.getClass().getMethod("getPrimaryClip", String.class, String.class, Integer.TYPE, Integer.TYPE, Boolean.TYPE);
                                    this.getMethodVersion = 4;
                                } catch (NoSuchMethodException e5) {
                                    this.getPrimaryClipMethod = this.manager.getClass().getMethod("getPrimaryClip", String.class, String.class, String.class, String.class, Integer.TYPE, Integer.TYPE, Boolean.TYPE);
                                    this.getMethodVersion = 5;
                                }
                            }
                        }
                    }
                }
            }
        }
        return this.getPrimaryClipMethod;
    }

    private Method getSetPrimaryClipMethod() throws NoSuchMethodException {
        if (this.setPrimaryClipMethod == null) {
            if (Build.VERSION.SDK_INT < 29) {
                this.setPrimaryClipMethod = this.manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class);
            } else {
                try {
                    this.setPrimaryClipMethod = this.manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, Integer.TYPE);
                    this.setMethodVersion = 0;
                } catch (NoSuchMethodException e) {
                    try {
                        this.setPrimaryClipMethod = this.manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, String.class, Integer.TYPE);
                        this.setMethodVersion = 1;
                    } catch (NoSuchMethodException e2) {
                        try {
                            this.setPrimaryClipMethod = this.manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, String.class, Integer.TYPE, Integer.TYPE);
                            this.setMethodVersion = 2;
                        } catch (NoSuchMethodException e3) {
                            this.setPrimaryClipMethod = this.manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, String.class, Integer.TYPE, Integer.TYPE, Boolean.TYPE);
                            this.setMethodVersion = 3;
                        }
                    }
                }
            }
        }
        return this.setPrimaryClipMethod;
    }

    private static ClipData getPrimaryClip(Method method, int methodVersion, IInterface manager) throws ReflectiveOperationException {
        if (Build.VERSION.SDK_INT < 29) {
            return (ClipData) method.invoke(manager, "com.android.shell");
        }
        switch (methodVersion) {
            case 0:
                return (ClipData) method.invoke(manager, "com.android.shell", 0);
            case 1:
                return (ClipData) method.invoke(manager, "com.android.shell", null, 0);
            case 2:
                return (ClipData) method.invoke(manager, "com.android.shell", null, 0, 0);
            case 3:
                return (ClipData) method.invoke(manager, "com.android.shell", 0, null);
            case 4:
                return (ClipData) method.invoke(manager, "com.android.shell", null, 0, 0, true);
            default:
                return (ClipData) method.invoke(manager, "com.android.shell", null, null, null, 0, 0, true);
        }
    }

    private static void setPrimaryClip(Method method, int methodVersion, IInterface manager, ClipData clipData) throws ReflectiveOperationException {
        if (Build.VERSION.SDK_INT < 29) {
            method.invoke(manager, clipData, "com.android.shell");
        }
        switch (methodVersion) {
            case 0:
                method.invoke(manager, clipData, "com.android.shell", 0);
                break;
            case 1:
                method.invoke(manager, clipData, "com.android.shell", null, 0);
                break;
            case 2:
                method.invoke(manager, clipData, "com.android.shell", null, 0, 0);
                break;
            default:
                method.invoke(manager, clipData, "com.android.shell", null, 0, 0, true);
                break;
        }
    }

    public CharSequence getText() {
        try {
            Method method = getGetPrimaryClipMethod();
            ClipData clipData = getPrimaryClip(method, this.getMethodVersion, this.manager);
            if (clipData != null && clipData.getItemCount() != 0) {
                return clipData.getItemAt(0).getText();
            }
            return null;
        } catch (ReflectiveOperationException e) {
            Ln.i("Could not invoke method" + e);
            return null;
        }
    }

    public boolean setText(CharSequence text) {
        try {
            Method method = getSetPrimaryClipMethod();
            ClipData clipData = ClipData.newPlainText(null, text);
            setPrimaryClip(method, this.setMethodVersion, this.manager, clipData);
            return true;
        } catch (ReflectiveOperationException e) {
            Ln.i("Could not invoke method:" + e);
            return false;
        }
    }

    private static void addPrimaryClipChangedListener(Method method, int methodVersion, IInterface manager, IOnPrimaryClipChangedListener listener) throws ReflectiveOperationException {
        if (Build.VERSION.SDK_INT < 29) {
            method.invoke(manager, listener, "com.android.shell");
        }
        switch (methodVersion) {
            case 0:
                method.invoke(manager, listener, "com.android.shell", 0);
                break;
            case 1:
                method.invoke(manager, listener, "com.android.shell", null, 0);
                break;
            default:
                method.invoke(manager, listener, "com.android.shell", null, 0, 0);
                break;
        }
    }

    private Method getAddPrimaryClipChangedListener() throws NoSuchMethodException {
        if (this.addPrimaryClipChangedListener == null) {
            if (Build.VERSION.SDK_INT < 29) {
                this.addPrimaryClipChangedListener = this.manager.getClass().getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class);
            } else {
                try {
                    this.addPrimaryClipChangedListener = this.manager.getClass().getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, Integer.TYPE);
                    this.addListenerMethodVersion = 0;
                } catch (NoSuchMethodException e) {
                    try {
                        this.addPrimaryClipChangedListener = this.manager.getClass().getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, String.class, Integer.TYPE);
                        this.addListenerMethodVersion = 1;
                    } catch (NoSuchMethodException e2) {
                        this.addPrimaryClipChangedListener = this.manager.getClass().getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, String.class, Integer.TYPE, Integer.TYPE);
                        this.addListenerMethodVersion = 2;
                    }
                }
            }
        }
        return this.addPrimaryClipChangedListener;
    }

    public boolean addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
        try {
            Method method = getAddPrimaryClipChangedListener();
            addPrimaryClipChangedListener(method, this.addListenerMethodVersion, this.manager, listener);
            return true;
        } catch (ReflectiveOperationException e) {
            Ln.i("Could not invoke method" + e);
            return false;
        }
    }
}